<?php
/** Unit_patient_location
	* 
	* @package Pharmacy Intervention 
	* @subpackage Controller
	* @category Setup 
	* @author Ray Nowell 
	*/  
class Unit_patient_location extends MY_Controller {

	function Unit_patient_location()
	{
		parent::MY_Controller();
	}
	
	//Private Functions:
	function _get_form_post_data($add_record = false)
	{

		$data = array('unit_patient_location_id' 	=>	$this->input->post('unit_patient_location_id'),
						'unit_patient_location'		=>	$this->input->post('unit_patient_location'),
						'active'		=>	$this->input->post('active'),
						'facility_id'	=>	$this->current_facility_id,
						'modified_date'	=>	date("Y-m-d H:i:s"),
						'modified_by'	=>	$this->ion_auth->get_user()->username,
					);
    
    if ($add_record)
      array_shift($data);
	
		return $data;
	}
	
	function _get_additional_javascript()
	{
		$ret_val = '$("#_unitPatientLocation").focus();';
		return $ret_val; 
	}
	
	function add()
	{
		//$this->output->enable_profiler(TRUE);
		$this->load->helper('form');
		$this->load->helper('date');
		$this->load->model('Unit_patient_location_model');

		$data['title'] = COMPANY_NAME." -- Add Unit Patient Location ";
		$data['heading'] = "Unit Patient Location Form";
	
		//Set the values for the Unit_patient_location View
		$data['unit_patient_location_id'] = '';
		$data['unit_patient_location'] = '';	
		$active_checkbox = array(
							    'name'        => 'active',
							    'id'          => '_active',
							    'value'       => '1',
							    'checked'     => '1',
							    'class'		=>	'editable',
				    		);
		$data['active_checkbox']	=	$active_checkbox;
		$data['created_date'] = '';
		$data['modified_date'] = '';
		
		$data['additional_javascript_doc_ready_functions'] = $this->_get_additional_javascript();

		$data['user_facility_name'] = $this->current_facility_name;
		
		$data['save_type'] = 'save_new';
				
		$this->load->view('unit_patient_location_view', $data);	 
		
	}
	 
	function edit()
	{
		$this->load->helper('date');
		$this->load->model('Unit_patient_location_model');
		
		//$this->output->enable_profiler(TRUE); 

		$data['title'] = COMPANY_NAME." -- Unit Patient Location Edit";
		$data['heading'] = "Unit Patient Location Edit";
		
		//The number of segments will determine the edit mode of the intervention:	single-list or multi-record navigation
		$total_segments = $this->uri->total_segments();
		
		if ($total_segments == SINGLE_EDIT)
		{
			$unit_patient_location_id = $this->uri->segment(3, 0);	//Three is the Intervention Id.	zero is the default value
			$data['additional_javascript_doc_ready_functions'] = $this->_get_additional_javascript().get_save_javascript('unit_patient_location');
		}			
		else 
		{
			$segment_to_get = $this->uri->segment(3);
				
			$unit_patient_location_id = $this->uri->segment($segment_to_get);
			
			$previous_segment = $segment_to_get <= 4 ? 4 : $segment_to_get - 1;
			$next_segment = $segment_to_get == $total_segments	? $total_segments : $segment_to_get + 1;
			$fullUri = $this->uri->uri_string();
			$search_text = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3);
			$replace_text = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'|';
			$current_uri = str_replace($search_text, $replace_text, $fullUri);
			$nav_url = base_url().'index.php'.$current_uri;
			$data['additional_javascript_doc_ready_functions'] = get_navigation_doc_ready_javascript().$this->_get_additional_javascript().get_save_javascript('unit_patient_location');
			$data['additional_jquery_javascript'] = get_navigation_functions($nav_url, $previous_segment, $next_segment, $total_segments);
			$data['nav_bar'] = get_navigation_markup(base_url(), $segment_to_get - 3, $total_segments - 3);
			
		}

		$unit_patient_location_row = $this->Unit_patient_location_model->get_unit_patient_location_record($unit_patient_location_id);
		
		//Set the values for the Unit_patient_location View
		
		$data['unit_patient_location_id']	=	$unit_patient_location_row->unit_patient_location_id;
		$data['unit_patient_location']		=	$unit_patient_location_row->unit_patient_location;
		$active_checkbox = array(
							    'name'        => 'active',
							    'id'          => '_active',
							    'value'       => '1',
							    'checked'     => $unit_patient_location_row->active,
							    'class'		=>	'editable',
				    		);
		$data['active_checkbox']	=	$active_checkbox;
		$data['created_date']				=	$unit_patient_location_row->created_date;
		$data['modified_date']				=	$unit_patient_location_row->modified_date;

		$data['user_facility_name'] = $this->current_facility_name;

		$data['save_type'] = 'update';
				
		$this->load->view('unit_patient_location_view', $data);	 
	}
		
	/** 
	 * Calls the Unit_patient_location Model to save the unit_patient_location based on the post data passed
	 * @access public */
	function save_new()
	{
		$this->load->model('Unit_patient_location_model');
				
		$data = $this->_get_form_post_data(true);
		
		$data['created_date'] = date('Y-m-d H:i:s');
		$data['created_by'] = $this->ion_auth->get_user()->username;
		
		$this->Unit_patient_location_model->Add_unit_patient_location($data);
		redirect('unit_patient_location/add');
	}
	
	/** 
	 * Calls the Unit_patient_location Model to update the unit_patient_location based on the post data passe
	 * @access public */
	function update()
	{
		$uri	=	$this->input->post('uri');
		$this->load->model('Unit_patient_location_model');
		$data = $this->_get_form_post_data();
		$this->Unit_patient_location_model->Update_unit_patient_location($data);
		redirect($uri);
	}
}

/* End of file unit_patient_location.php */
/* Location: ./system/application/controllers/unit_patient_location.php */
